var appInstance = getApp().globalData;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: "",
    imgurl: "../../../images/head.jpg",
    isshiming: 0,
    btnItem: "",
    showbtns: {
      noToken: 0,
      toresume: "showLogin",
      towallet: "showLogin",
      topoint: "showLogin",
      myjob: "showLogin",
      totask: "showLogin",
      tojob: "showLogin"

    },
    showInfo: {
      name: "请登录",
      wallet: 0.00,
      resume: "0%",
      point: "0"
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    const that = this;
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }
  },
  onShow: function(){
    const that = this;
    // 获取用户信息，若token存在，则去获取，若不存在则意味着登陆过期了重新需要授权。
    let token = wx.getStorageSync("token");
    if (token) {
      // 获取个人简历的信息，若没有简历直接默认值
      getApp().globalData.get("/api/resume", {}).then(res => {
        if (res.code == 200){
          appInstance.indexid = res.data.resume.id;
          let progess = 0;
          if (res.data.resume.tel != "") {
            progess = progess + 20;
          }
          if (res.data.educationals.length != 0) {
            progess = progess + 20;
          }
          if (res.data.workExperiences.length != 0) {
            progess = progess + 20;
          }
          if (res.data.certificates.length != 0) {
            progess = progess + 20;
          }
          if (res.data.resume.comment != "") {
            progess = progess + 20;
          }
          that.data.showInfo.resume = progess + "%"
          that.data.showInfo.name = res.data.resume.name
          that.setData({
            imgurl: res.data.resume.avatar,
            showbtns: {
              noToken: 1,
              toresume: "toresume",
              towallet: "towallet",
              topoint: "topoint",
              myjob: "myjob",
              totask: "totask",
              tojob: "tojob"
            },
            showInfo: that.data.showInfo
          })
        }
      })
      // 获取认证信息
      getApp().globalData.get("/api/resume/personal/certification", {}).then(res => {
        if (res.data != null) {
          if (res.data.status == "REVIEWING"){
            that.data.isshiming = 1
          } else if (res.data.status == "FAILD"){
            that.data.isshiming = 0
          }else{
            that.data.isshiming = 2
          }
          that.setData({
            isshiming: that.data.isshiming,
          })
        }
      })
    }
  },
  toresume: function(){
    wx.navigateTo({
      url: './resume/resume',
    })
  },
  onGotUserInfo: function (res){//授权并获取登录信息
    const that = this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey,
        getApp().globalData.get("/api/wx/auth", {
          code: res.code
        }).then(res => {
          wx.setStorageSync('sessionKey', res.data.sessionKey)
        })
      }
    })
    getApp().globalData.get("/api/wx/info", {
      sessionKey: wx.getStorageSync("sessionKey"),
      signature: res.detail.signature,
      rawData: res.detail.rawData,
      encryptedData: res.detail.encryptedData,
      iv: res.detail.iv
    }).then(infores => {
      wx.setStorageSync('token', infores.data.token)
      
      // 获取个人简历的信息
      getApp().globalData.get("/api/resume", {}).then(res => {
        if (res.code == 200){
          appInstance.indexid = res.data.resume.id;
          let progess = 0;
          if (res.data.resume.tel != "") {
            progess = progess + 20;
          }
          if (res.data.educationals.length != 0) {
            progess = progess + 20;
          }
          if (res.data.workExperiences.length != 0) {
            progess = progess + 20;
          }
          if (res.data.certificates.length != 0) {
            progess = progess + 20;
          }
          if (res.data.resume.comment != "") {
            progess = progess + 20;
          }
          that.data.showInfo.resume = progess + "%"
          that.data.showInfo.name = res.data.resume.name
          that.setData({
            imgurl: res.data.resume.avatar,
            showbtns: {
              noToken: 1,
              toresume: "toresume",
              towallet: "towallet",
              topoint: "topoint",
              myjob: "myjob",
              totask: "totask",
              tojob: "tojob"
            },
            showInfo: that.data.showInfo
          })
        } else if (res.code == 2002){
          that.data.showInfo.name = infores.data.nickName;
          that.setData({
            imgurl: infores.data.avatarUrl,
            showbtns: {
              noToken: 1,
              toresume: "toresume",
              towallet: "towallet",
              topoint: "topoint",
              myjob: "myjob",
              totask: "totask",
              tojob: "tojob"
            },
            showInfo: that.data.showInfo
          })
        }
      })
      that.setData({
        showbtns: {
          noToken: 1,
          toresume: "toresume",
          towallet: "towallet",
          topoint: "topoint",
          myjob: "myjob",
          totask: "totask",
          tojob: "tojob"
        },
      })
      // 获取认证信息
      getApp().globalData.get("/api/resume/personal/certification", {}).then(res => {
        if (res.data != null) {
          if (res.data.status == "REVIEWING") {
            that.data.isshiming = 1
          } else if (res.data.status == "FAILD") {
            that.data.isshiming = 0
          } else {
            that.data.isshiming = 2
          }
          that.setData({
            isshiming: that.data.isshiming,
          })
        }
      })
    })
  },
  towallet: function() {
    wx.showModal({
      title: '提示',
      content: '功能暂未开放',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  topoint: function() {
    wx.navigateTo({
      url: './point/point',
    })
  },
  myjob: function(e) {
    console.log(e)
    wx.navigateTo({
      url: './myjob/myjob?pageindex=' + e.currentTarget.dataset.type,
    })
  },
  totask: function(e){
    wx.navigateTo({
      url: './mytask/mytask',
    })
  },
  tojob: function(e){
    wx.redirectTo({
      url: '/pages/release/default/default',
    })
    wx.hideTabBar();
  },
  authentication: function() {
    wx.navigateTo({
      url: './authentication/authentication',
    })
  },
  showLogin: function(){
    this.setData({
      show:"show",
    })
  },
  getShouQuan:function(){
    this.setData({
      show: "",
    })
  },
  cancle:function(){
    this.setData({
      show: "",
    })
    wx.showToast({
      title: '已取消授权',
      icon: "none"
    })
  },
  person_esc: function(){
    wx.clearStorage();
    wx.switchTab({
      url: '/pages/recruit/personal/personal',
    })
  }
})